package gwt.trading.client;

import gwt.trading.client.geometry.Point;
import gwt.trading.client.util.Closure;
import gwt.trading.client.util.ColorEnum;
import gwt.trading.client.util.GwtList;

import com.google.gwt.core.client.GWT;

/**
 * @author Iulian Costan (iulian.costan@gmail.com)
 *
 */
public class LinePriceChart extends PriceChart {

	public LinePriceChart(StockQuoteList quotes, ColorEnum color) {
		super(quotes, color);
	}
	
	public void draw(final Renderer renderer) {
		GWT.log("drawing linechart ...", null);
		
		GwtList quotes = getQuoteList().getQuotes();
		
		// start line graph
		Point startPoint = transform((StockQuote) quotes.getFirst());
		renderer.startLine(startPoint, getColor());
		
		// intermediate lines
		quotes.forAllExceptEnds(new Closure() {

			public void execute(Object object) {
				Point next = transform((StockQuote) object);
				renderer.continueLine(next);
			}
		});
		
		// close line graph
		Point endPoint = transform((StockQuote) quotes.getLast());
		renderer.finishLine(endPoint);
	}

	private Point transform(StockQuote quote) {
		int index = quote.getIndex();
		float close = quote.getClose();
		
		int xPosition = getDrawingManager().getXPosition(index);
		int yPosition = getDrawingManager().getYPosition(close);
		
		return new Point(xPosition, yPosition);
	}
	
}
